using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace SiteLib.Data
{
	[DataObject]
	[Serializable]
	public partial class SiteContentType
	{
		#region Constants

		private const string TableName = "[dbo].[SiteContentType]";

		#endregion
	
		#region Properties

		public int? SiteContentTypeId { get; set; }
		public string Title { get; set; }
        public bool? IsShortList { get; set; }
        public bool? IsDeleted { get; set; }

		#endregion
	
		#region Methods
	
		#region Mapping Methods
	
		private void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);
		
			ds.Tables[TableName].Columns.Add("SiteContentTypeId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("Title", typeof(String));
            ds.Tables[TableName].Columns.Add("IsShortList", typeof(Boolean));
            ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean));
		
			DataRow dr = ds.Tables[TableName].NewRow();

			dr["SiteContentTypeId"] = SiteContentTypeId ?? (object) DBNull.Value;
			dr["Title"] = Title ?? (object) DBNull.Value;
            dr["IsShortList"] = IsShortList ?? (object)DBNull.Value;
            dr["IsDeleted"] = IsDeleted ?? (object)DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}
	
		private void MapFrom(DataRow dr)
		{
			SiteContentTypeId = dr["SiteContentTypeId"] != DBNull.Value ? Convert.ToInt32(dr["SiteContentTypeId"]) : SiteContentTypeId = null;
			Title = dr["Title"] != DBNull.Value ? Convert.ToString(dr["Title"]) : Title = null;
            IsShortList = dr["IsShortList"] != DBNull.Value ? Convert.ToBoolean(dr["IsShortList"]) : IsShortList = null;
            IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
		}

		private static List<SiteContentType> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<SiteContentType> objects = new List<SiteContentType>();
		
			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");
			
			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;
		
			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[SiteContentType] in DataSet.");
		
			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[SiteContentType] is empty.");
		
			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				SiteContentType instance = new SiteContentType();
				instance.MapFrom(dr);
				objects.Add(instance);
			}
		
			// Return collection.
			return objects;
		}
	
		#endregion
	
		#region CRUD Methods
	
		public static SiteContentType Get(Int32? siteContentTypeId)
		{
			SiteContentType instance = new SiteContentType();
		
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentTypeId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get SiteContentType ID:{0} from Database.", siteContentTypeId));
					ds.Tables[0].TableName = TableName;
		
					instance.MapFrom(ds.Tables[0].Rows[0]);
				}
			}
			return instance;
		}
	
		#region INSERT
        public void Insert(String title, Boolean? isShortList, Boolean? isDeleted, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_INSERT";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, title, isShortList, isDeleted))
			{
				SiteContentTypeId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
			}
		}
        private void Insert(DbTransaction transaction = null)
		{
			Insert(Title, IsShortList, IsDeleted, transaction);
		}

		#endregion
	
		#region UPDATE
        private static void Update(Int32? siteContentTypeId, String title, Boolean? isShortList, Boolean? isDeleted, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@siteContentTypeId"].Value = siteContentTypeId;
				dbCommand.Parameters["@title"].Value = title;
                dbCommand.Parameters["@isShortList"].Value = isShortList;
                dbCommand.Parameters["@isDeleted"].Value = isDeleted;
		
				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}

	    private static void Update(SiteContentType siteContentType)
		{
			siteContentType.Update();
		}
	
		private static void Update(SiteContentType siteContentType, DbTransaction transaction)
		{
			siteContentType.Update(transaction);
		}
		
		public void Update(DbTransaction transaction = (DbTransaction)null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@siteContentTypeId"].SourceColumn = "SiteContentTypeId";
				dbCommand.Parameters["@title"].SourceColumn = "Title";
                dbCommand.Parameters["@isShortList"].SourceColumn = "IsShortList";
                dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";

				using (DataSet ds = new DataSet())
				{
					MapTo(ds);
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}

	    #endregion
	
		#region DELETE

		private static void Delete(Int32? siteContentTypeId, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentTypeId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}

		private void Delete(DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, SiteContentTypeId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
			SiteContentTypeId = null;
		}

	    #endregion
	
		#region SEARCH

        private static List<SiteContentType> Search(Int32? siteContentTypeId = null, String title = null, Boolean? isShortList = null, Boolean? isDeleted = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteContentType_SEARCH";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentTypeId, title, isShortList, isDeleted))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		private static List<SiteContentType> Search(SiteContentType searchObject)
		{
			return Search (searchObject.SiteContentTypeId, searchObject.Title, searchObject.IsShortList, searchObject.IsDeleted);
		}
	
		#endregion
	
		#endregion
	
		#endregion
	}
}

